package com.zxy.service;

import com.zxy.dao.RolePermissionsDao;
import com.zxy.dao.RolesDao;
import com.zxy.dao.UserRolesDao;
import com.zxy.model.RolePermissions;
import com.zxy.model.Roles;
import com.zxy.model.RolesQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RolesService {
    @Resource
    private RolesDao rolesDao;
    @Resource
    private UserRolesDao userRolesDao;
    @Resource
    private RolePermissionsDao rolePermissionsDao;

    @Transactional
    public int delete(Integer id) {
        userRolesDao.deleteByRoleId(id);
        rolePermissionsDao.deleteByRoleId(id);
        return rolesDao.delete(id);
    }


    public int insert(Roles record) {
        return rolesDao.insert(record);
    }


    public List<Roles> select(RolesQuery rolesQuery) {
        return rolesDao.select(rolesQuery);
    }

    public int update(Roles record) {
        return rolesDao.update(record);
    }

    public int editPermission(RolePermissions model) {
        if (model.isHas()) {
            return rolePermissionsDao.deleteByPermissionId(model.getPermissionId());
        }
        return rolePermissionsDao.insert(model);
    }
}
